package com.haixiaoke.business;

import cn.hutool.core.util.ObjectUtil;
import cn.hutool.core.util.StrUtil;
import com.haixiaoke.common.core.domain.AjaxResult;
import com.haixiaoke.common.core.page.TableDataInfo;
import com.haixiaoke.common.utils.poi.ExcelUtil;
import com.haixiaoke.domain.SmartLock;
import com.haixiaoke.domain.SmartLockFace;
import com.haixiaoke.service.ISmartLockFaceService;
import com.haixiaoke.service.impl.SmartLockServiceImpl;
import com.haixiaoke.smartLock.haixiaoke.SmartLockHXKEventHandleService;
import com.haixiaoke.smartLock.huixiangjia.SmartLockHxjEventHandleService;
import com.haixiaoke.smartLock.youdian.SmartLockYdEventHandleService;
import com.haixiaoke.system.controller.BaseController;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import java.util.List;

/**
 * 智能锁授权人脸信息Controller
 *
 * @author xiaolong
 * @date 2023-06-10
 */
@RestController
@RequestMapping("/business/face")
public class SmartLockFaceController extends BaseController {
    @Autowired
    private ISmartLockFaceService smartLockFaceService;
    @Autowired
    private SmartLockYdEventHandleService smartLockYdEventHandleService;
    @Autowired
    private SmartLockHXKEventHandleService smartLockHXKEventHandleService;
    @Autowired
    private SmartLockHxjEventHandleService smartLockHxjEventHandleService;
    @Autowired
    private SmartLockServiceImpl smartLockService;

    /**
     * 查询智能锁授权人脸信息列表
     */
    @GetMapping("/list")
    public TableDataInfo list(SmartLockFace smartLockFace) {
        startPage();
        List<SmartLockFace> list = smartLockFaceService.selectSmartLockFaceList(smartLockFace);
        return getDataTable(list);
    }

    /**
     * 导出智能锁授权人脸信息列表
     */
    @PostMapping("/export")
    public void export(HttpServletResponse response, SmartLockFace smartLockFace) {
        List<SmartLockFace> list = smartLockFaceService.selectSmartLockFaceList(smartLockFace);
        ExcelUtil<SmartLockFace> util = new ExcelUtil<SmartLockFace>(SmartLockFace.class);
        util.exportExcel(response, list, "智能锁授权人脸信息数据");
    }

    /**
     * 获取智能锁授权人脸信息详细信息
     */
    @GetMapping(value = "/{faceId}")
    public AjaxResult getInfo(@PathVariable("faceId") Long faceId) {
        return success(smartLockFaceService.selectSmartLockFaceByFaceId(faceId));
    }

    /**
     * 新增智能锁授权人脸信息
     */
    @PostMapping
    public AjaxResult add(@RequestBody SmartLockFace smartLockFace) {
        SmartLock smartLock = smartLockService.selectBySn(smartLockFace.getSn());
        if (smartLock.getBrandId() == SmartLock.THIRD_GENERATION_SMART_LOCK) {
            return AjaxResult.error("该锁不支持人脸授权");
        } else if (smartLock.getBrandId() == SmartLock.FOUR_GENERATION_SMART_LOCK) {
            return AjaxResult.error("该锁不支持人脸授权");
        } else if (smartLock.getBrandId() == SmartLock.FIVE_GENERATION_SMART_LOCK) {
            return toAjax(smartLockHXKEventHandleService.insertSmartLockFace(smartLockFace));
        } else if (smartLock.getBrandId() == SmartLock.SIX_GENERATION_SMART_LOCK){
            return toAjax(smartLockYdEventHandleService.insertSmartLockFace(smartLockFace));
        } else if (smartLock.getBrandId() == SmartLock.SEVEN_GENERATION_SMART_LOCK) {
            return toAjax(smartLockHxjEventHandleService.insertSmartLockFace(smartLockFace));
        } else {
            return AjaxResult.error("该锁不支持人脸授权");
        }
    }

    /**
     * 修改智能锁授权人脸信息
     */
    @PutMapping
    public AjaxResult edit(@RequestBody SmartLockFace smartLockFace) {
        String sn = smartLockFace.getSn();
        String identification = smartLockFace.getIdentification();
        SmartLock smartLock = smartLockService.selectBySn(sn);
        if (ObjectUtil.isEmpty(smartLock)) {
            return AjaxResult.error("查不到该锁");
        }
        if (SmartLock.SIX_GENERATION_SMART_LOCK == smartLock.getBrandId()) {
            //smartLockYdEventHandleService.deleteSmartLockFace(returnFaceId);
        } else if (SmartLock.FIVE_GENERATION_SMART_LOCK == smartLock.getBrandId()) {
            if (StrUtil.isEmpty(smartLockFace.getSn()) || StrUtil.isEmpty(smartLockFace.getIdentification())) {
                return AjaxResult.error("数据缺失");
            }
            smartLockHXKEventHandleService.deleteSmartLockFace(sn,identification);
        } else if (SmartLock.SEVEN_GENERATION_SMART_LOCK == smartLock.getBrandId()) {
            if (StrUtil.isEmpty(sn) || StrUtil.isEmpty(identification)) {
                return AjaxResult.error("数据缺失");
            }
            smartLockHxjEventHandleService.deleteSmartLockFace(sn,identification);
        } else {
            return AjaxResult.error("该锁不支持人脸授权");
        }

        //新增人脸授权
        if (smartLock.getBrandId() == SmartLock.THIRD_GENERATION_SMART_LOCK) {
            return AjaxResult.error("该锁不支持人脸授权");
        } else if (smartLock.getBrandId() == SmartLock.FOUR_GENERATION_SMART_LOCK) {
            return AjaxResult.error("该锁不支持人脸授权");
        } else if (smartLock.getBrandId() == SmartLock.FIVE_GENERATION_SMART_LOCK) {
            return toAjax(smartLockHXKEventHandleService.insertSmartLockFace(smartLockFace));
        } else if (smartLock.getBrandId() == SmartLock.SIX_GENERATION_SMART_LOCK){
            return toAjax(smartLockYdEventHandleService.insertSmartLockFace(smartLockFace));
        } else if (smartLock.getBrandId() == SmartLock.SEVEN_GENERATION_SMART_LOCK) {
            return toAjax(smartLockHxjEventHandleService.insertSmartLockFace(smartLockFace));
        } else {
            return AjaxResult.error("该锁不支持人脸授权");
        }
    }

    /**
     * 删除智能锁授权人脸信息
     */
    @DeleteMapping("/{faceIds}")
    public AjaxResult remove(@PathVariable Long[] faceIds) {
        return toAjax(smartLockFaceService.deleteSmartLockFaceByFaceIds(faceIds));
    }

    /**
     * 通过SN号获取智能锁人脸权限信息
     *
     * @param sn 智能锁SN
     * @return
     */
    @GetMapping(value = "/info/{sn}")
    public AjaxResult getFaceList(@PathVariable("sn") String sn) {
        SmartLockFace smartLockFace = new SmartLockFace();
        smartLockFace.setSn(sn);
        return success(smartLockFaceService.selectSmartLockFaceList(smartLockFace));

    }

    /**
     * 删除智能锁人脸授权信息和删除人脸授权
     * returnFaceId 厂商授权id
     *
     * @return
     */
    @DeleteMapping("/remove")
    public AjaxResult remove(@RequestParam(value = "returnFaceId", required = false) Long returnFaceId,
                             @RequestParam(value = "sn") String sn,
                             @RequestParam(value = "identification", required = false) String identification) {
        SmartLock smartLock = smartLockService.selectBySn(sn);
        if (ObjectUtil.isEmpty(smartLock)) {
            return AjaxResult.error("查不到该锁");
        }
        if (SmartLock.SIX_GENERATION_SMART_LOCK == smartLock.getBrandId()) {
            return toAjax(smartLockYdEventHandleService.deleteSmartLockFace(returnFaceId));
        } else if (SmartLock.FIVE_GENERATION_SMART_LOCK == smartLock.getBrandId()) {
            if (StrUtil.isEmpty(sn) || StrUtil.isEmpty(identification)) {
                return AjaxResult.error("数据缺失");
            }
            return toAjax(smartLockHXKEventHandleService.deleteSmartLockFace(sn,identification));
        } else if (SmartLock.SEVEN_GENERATION_SMART_LOCK == smartLock.getBrandId()) {
            if (StrUtil.isEmpty(sn) || StrUtil.isEmpty(identification)) {
                return AjaxResult.error("数据缺失");
            }
            return toAjax(smartLockHxjEventHandleService.deleteSmartLockFace(sn,identification));
        } else {
            return AjaxResult.error("该锁不支持人脸授权");
        }

    }
}
